#include "../include/lib_headers.h"
int splits_directory(char *string1, char *store[SIZE], char *split_char1) {

	int i = 1, j = 0;
	char *temp, *string, *split_char;
	
	if(strcmp(string1,"/") == 0){
		return 0;
	}
	
	string = (char*) malloc(sizeof(char)*SIZE);
	strcpy(string, string1);
	split_char = (char*) malloc(sizeof(char));
	strcpy(split_char, split_char1);
	
	temp = strtok(string, split_char);
	store[0] = (char*) malloc(sizeof(char)*SIZE);
	strcpy(store[0], temp);

	while ((temp = strtok(NULL, split_char)) != NULL) {
		store[i] = (char*) malloc(sizeof(char)*SIZE);
		strcpy(store[i], temp);
		i++;
	}

	free(string);
	free(split_char);
	return i;
}

int check_character(char *s, char ch){
	int i=0;
	while(s[i] != '\0'){		
		if(s[i] == ch )
			return SUCCESS;
		i++;
	}
	return FAILURE;
}

char* file_extension(char *filename){
	char *output_message=(char*)malloc(sizeof(char)*SIZE);
	strcpy(output_message, "");
	char *filetype[SIZE];
	int i=0;
	
	i = splits_directory(filename, filetype, ".");
	/* printf( "i = %d\n", i); */
	if(i != 0){
		strcpy(output_message, filetype[i-1]);
	}		
	
	return output_message;
}

char* strip_name(char *t, char *c) {
	int i = 0, j = 0;
	char *p[SIZE];
	char s[SIZE] = "";
	char *string = (char*) malloc(sizeof(char) * SIZE);

	strcpy(s, add_slash(t));
	fflush(stdout);
	i = splits_directory(s, p, "/");
	while (j < i) {
		fflush(stdout);
		if (strcmp(c, p[j]) == 0)
			break;
		j++;
	}
	strcpy(string, "/");
	strcat(string, p[j++]);
	/* strcat(string, "/"); */
	while (j < i) {
		strcat(string, p[j++]);
		strcat(string, "/");
	}
	return string;
}

int validate_name1(char *str){	
	int i=0;
	while(str[i]!='\0'){
		if(!isalnum(str[i])&&!(str[i]=='.'||str[i]=='_')){
			return FAILURE;	
		}
		if(i==0){
			if(str[i]=='.'){
				return FAILURE;
			}
		}
	i++;		
	}	
	return SUCCESS;	
}

int validate_name(char *str){
	int i = 0;
	while(str[i] != '\0'){
		if(str[i] == '/')
			return FAILURE;
		i++;		
	}
	return SUCCESS;
}
